package com.white.algebra.controller;


import com.white.algebra.domain.Pager;
import com.white.algebra.domain.Syslog;
import com.white.algebra.service.SysLogService;
import com.white.algebra.service.impl.SysLogServiceImpl;
import com.white.algebra.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class SysLogController extends BaseServlet{
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        SysLogService sysLogService=new SysLogServiceImpl();
        int page= WebUtils.parseInt(req.getParameter("page"),1);
        int size= WebUtils.parseInt(req.getParameter("size"),4);
        if(page<1){
            page=1;
        }
        String username = req.getParameter("username");
        int startRecord=(page-1)*size;
        List<Syslog> sysLog = sysLogService.findAll(startRecord,size,username);
        int recordCount=sysLogService.getLogNum(username);
        Pager<Syslog> pager=new Pager<>();
        pager.calculate(sysLog,page,size,recordCount);

        req.setAttribute("page",pager);
        req.setAttribute("username",username);
        req.getRequestDispatcher("/pages/syslog-list.jsp").forward(req,resp);
    }


    protected void deleteSysLog(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        SysLogService sysLogService=new SysLogServiceImpl();
        sysLogService.deleteSysLog();
        req.getRequestDispatcher("/pages/syslog-list.jsp").forward(req,resp);
    }
}
